package xyz.zhzby.cyzx.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import xyz.zhzby.cyzx.security.entity.User;
import xyz.zhzby.cyzx.security.util.CyzxSecurityContextHolder;
import xyz.zhzby.cyzx.servicebase.domain.DataLogInfo;
import xyz.zhzby.cyzx.servicebase.domain.RestResponse;
import xyz.zhzby.cyzx.ucenter.entity.UserInterest;
import xyz.zhzby.cyzx.ucenter.mapper.UserInterestMapper;
import xyz.zhzby.cyzx.ucenter.service.IUserInterestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户兴趣关联表 服务实现类
 * </p>
 *
 * @author zby
 * @since 2021-12-16
 */
@Service
public class UserInterestServiceImpl extends ServiceImpl<UserInterestMapper, UserInterest> implements IUserInterestService {

    @Override
    public RestResponse<String> updateInterests(List<Long> ids) {
        User subject = CyzxSecurityContextHolder.getSubject();
        LambdaQueryWrapper<UserInterest> qw = new LambdaQueryWrapper<>();
        qw.eq(UserInterest::getUserId, subject.getId());
        baseMapper.delete(qw);

        List<UserInterest> userInterestList = new ArrayList<>();
        for (Long interestId : ids) {
            UserInterest userInterest = new UserInterest();
            userInterest.setInterestId(interestId);
            userInterest.setUserId(subject.getId());
            subject.fillCreateInfo(userInterest, DataLogInfo.USER_FIELD_ACTUAL_NAME);
            userInterestList.add(userInterest);
        }
        this.saveBatch(userInterestList);
        return RestResponse.success("更新成功");
    }
}
